package com.zhigongtech.tender.sys.service.impl;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhigongtech.common.exception.NomalException;
import com.zhigongtech.extension.entry.vo.AccountVo;
import com.zhigongtech.tender.sys.entity.User;
import com.zhigongtech.tender.sys.mapper.UserMapper;
import com.zhigongtech.tender.sys.service.IUserService;

/**
 * <p>
 * SYS_USER 系统用户 服务实现类
 * </p>
 *
 * @author Alan
 * @since 2019-05-28
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Override
	public AccountVo getOneByAccount(String account) throws NomalException {
		return this.baseMapper.getOneByAccount(account);
	}

	@Override
	public User getUserByAccount(String account) throws NomalException {
		return this.baseMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getAccount, account));
	}

	/**   
	 * <p>Title: getEncryptedPassword</p>   
	 * <p>Description: </p>   
	 * @param password 明文密码
	 * @param salt  盐，可以使用 BCrypt.gensalt() 获取默认的盐
	 * @return 加密后的密码
	 * @throws NomalException   
	 * @see com.zhigongtech.tender.sys.service.IUserService#getEncryptedPassword(java.lang.String, java.lang.String)   
	 * @author: Alan
	 * @date:   2019-06-03 16:38:05
	 */
	@Override
	public String getEncryptedPassword(String password, String salt) throws NomalException {
		return BCrypt.hashpw(password, salt);
	}

	/**   
	 * <p>Title: checkPasswd</p>   
	 * <p>Description: </p>   
	 * @param newPwd  明文密码
	 * @param secretPwd 加密后的密码
	 * @return   
	 * @see com.zhigongtech.tender.sys.service.IUserService#checkPasswd(java.lang.String, java.lang.String)   
	 * @author: Alan
	 * @date:   2019-06-03 17:01:53
	 */
	@Override
	public boolean checkPasswd(String newPwd, String secretPwd) {
		return BCrypt.checkpw(newPwd, secretPwd);
	}

	/**   
	 * <p>Title: getNewSalt</p>   
	 * <p>Description: </p>   
	 * @return 盐值，默认使用 BCrypt.gensalt() 获取盐值
	 * @see com.zhigongtech.tender.sys.service.IUserService#getNewSalt()   
	 * @author: Alan
	 * @date:   2019-06-03 17:01:53
	 */
	@Override
	public String getNewSalt() {
		return BCrypt.gensalt();
	}

}
